﻿#ifndef _AVTYPE_H_
#define _AVTYPE_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdint.h>
#include <math.h>
#include <ctype.h>
#if defined(WIN32)
#define NOMINMAX
#include <winsock2.h>
#include <mstcpip.h>
#include <ws2ipdef.h>
#include <MSWSock.h>
#include <ws2tcpip.h>
#include <Iphlpapi.h>
#include <io.h>
#else
#include <semaphore.h>
#include <sys/prctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/uio.h>
#include <sys/uio.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <signal.h>
#include <dirent.h>
#include <netdb.h>
#include <errno.h>
#include <dlfcn.h>
#include <unistd.h>
#include <strings.h>
#include <pthread.h>
#endif
#include <stddef.h>

#ifdef __HuaweiLite__
#include "netif/ifaddrs.h"
#include "lwip/sockets.h"
#include "lwip/api.h"
#include "lwip/sys.h"
#include "lwip/igmp.h"
#include "lwip/inet.h"
#include "lwip/tcp.h"
#include "lwip/raw.h"
#include "lwip/udp.h"
#include "lwip/tcpip.h"
#include "lwip/pbuf.h"
#include "lwip/netif.h"
#include "lwip/ip_addr.h"
#include "lwip/dhcp.h"
#include "lwip/filter.h"
#endif /* __HuaweiLite__ */


#define LOG_MAX_BUFFER 256
#define LOG_MAX_FILE_LINES 1000
#define LOG_MAX_FILENAME_LENGTH 20
#define LOG_MAX_CACHE_ITEM_NUM 500


#ifndef ConfMaxCpuCore
#define ConfMaxCpuCore 32
#endif



#ifndef _ukProtoRequest
#define _ukProtoRequest				0x80000000
#endif
#ifndef _ukProtoRespond
#define _ukProtoRespond				0x40000000
#endif
#ifndef _ukProtoSet
#define _ukProtoSet					0x20000000
#endif
#ifndef _ukProtoGet
#define _ukProtoGet					0x10000000
#endif
#ifndef _ukProtoClear
#define _ukProtoClear				0x0fffffff
#endif
#ifndef _ukProtoClearMsg
#define _ukProtoClearMsg			0xf0000000
#endif



typedef enum {
	TLogNone = 0,
	TLogMsg,
	TLogWarn,
	TLogError,
	TLogHint,
	TLogClosePrint,
}ukLogT;

typedef struct {
	unsigned long long		UpTime;				//64bit 系统启动毫秒数
	unsigned long long		Timeval_tvSec;		//64bit	UTC 秒数  （gettimeofday)
	unsigned long long		Timeval_tvUsec;		//64bit 同上
	ukLogT					Type;				//32bit 类型
	int						FileLine;			//32bit 行号
	char					FileName[LOG_MAX_FILENAME_LENGTH];  //文件名
	char					Msg[LOG_MAX_BUFFER];	//MSG
}LogItem;


typedef struct {
	unsigned int CpuFrequency[ConfMaxCpuCore];
	unsigned int CpuFreeRate[ConfMaxCpuCore];
	unsigned int MemTotal;
	unsigned int MemFree;
}C_CpuMemRunStatus;

typedef struct {
	time_t				SystemUpTime;
	C_CpuMemRunStatus	CpuMemRunStatus;
}C_KeepAlive;

typedef struct {
	char			UID[32];
	char			UUID[64];
	char			MacAddr[32];			//eth0@00:11:22:33:44:09;wlan0@11:22:33:44:55:66-----
	unsigned long	TimeVal_TvSec;			//由服务器返回的服务器utc时间，如果本地时间相差太多，可以同步时间----
	unsigned long	TimeVal_TvUsec;
	unsigned long   LoopTimeStamp;
}C_Connect;


typedef struct {
	unsigned int AppVersion;
	char		 AppVersionString[64];
	char		 OEM[128];
	char		 Descriptor[1024];
	char		 BuildTime[64];
	char		 ProtoVersion[64];
	char		 KernelVersion[64];
	char		 FilesystemVerion[64];
	char		 GitCommitID[128];
	char		 GitCommitIDShort[32];
	char		 GitBranch[32];
}C_SystemVersion;

typedef enum {
	NetCommT_LAN = 0,
	NetCommT_Wireless = 1,
	NetCommT_SIM = 2,
	NetCommT_BlueTooth = 3,
	NetCommT_GPS = 4,

	NetCommT_LAN1 = 5,
	NetCommT_Wireless1 = 6,
	NetCommT_SIM1 = 7,
	NetCommT_LAST = 8,
}NetCommT;

typedef enum {
	NetCommGetMode_NONE = 0,
	NetCommGetMode_MANUAL = 1,
	NetCommGetMode_AUTO = 2,
	NetCommGetMode_PPPOE = 3,
	NetCommGetMode_LAST = 4
}NetCommGetMode;

typedef struct {
	NetCommT NetDevice;
	NetCommGetMode NetGetType;
	char IpAddr[32];
	char SubMask[32];
	char Mac[32];
	char GateWay[32];
	char DnsI[32];
	char DnsII[32];
}C_NetCommProfile;

















#if defined(_WINDOWS)
typedef HANDLE avMutexT;
typedef HANDLE avSemT;
typedef HANDLE avThreadT;
#elif defined(__linux__)
typedef pthread_mutex_t avMutexT;
typedef sem_t	avSemT;
typedef pthread_t avThreadT;
#endif

#if defined(_WINDOWS)
__inline int avMutexInit(avMutexT* Mutex) {
	*Mutex = CreateMutex(NULL, FALSE, NULL);
	return 0;
}

__inline int avMutexRelease(avMutexT* Mutex) {
	CloseHandle(*Mutex);
	return 0;
}

__inline int avMutexLock(avMutexT* Mutex) {
	DWORD Dret = WaitForSingleObject(*Mutex, INFINITE);
	return Dret == WAIT_ABANDONED ? 0 : -1;
}

__inline int avMutexUnLock(avMutexT* Mutex) {
	BOOL Bret = ReleaseMutex(*Mutex);
	return Bret == TRUE ? 0 : -1;
}

__inline int avThreadCreate(void* (*tsk)(void* args), void* args) {
	DWORD htid;
	HANDLE Hret = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)tsk, args, 0, &htid);
	return Hret == NULL ? -1 : 0;
}

__inline void avThreadDeteach() {
}

__inline avThreadT avThreadSelf() {
	return (avThreadT)GetCurrentThreadId();
}

#else

static inline int avMutexInit(avMutexT* Mutex) {
	return pthread_mutex_init(Mutex, NULL);
}

static inline int avMutexRelease(avMutexT* Mutex) {
	return pthread_mutex_destroy(Mutex);
}

static inline int avMutexLock(avMutexT* Mutex) {
	return pthread_mutex_lock(Mutex);
}

static inline int avMutexUnLock(avMutexT* Mutex) {
	return pthread_mutex_unlock(Mutex);
}

static inline int avThreadCreate(void* (*tsk)(void* args), void* args) {
	pthread_t tid;
	return pthread_create(&tid, NULL, tsk, args);
}

static inline void avThreadDeteach() {
	pthread_detach(pthread_self());
}

static inline avThreadT avThreadSelf() {
	return pthread_self();
}
#endif

//##TIME
#if defined(_WINDOWS)
#define AppRunMSec(msec) msec = (long)(clock()*(1000.0/CLOCKS_PER_SEC))
#define AvGetTimeOfDay(tv, tz) \
	do{ union {long long ns100;FILETIME ft;} now;								\
		GetSystemTimeAsFileTime(&now.ft);										\
		(tv)->tv_usec = (long)((now.ns100 / 10LL) % 1000000LL);					\
		(tv)->tv_sec = (long)((now.ns100 - 116444736000000000LL) / 10000000LL);	\
	} while (0)

#define AvLocalTimeS(_t,_tm) localtime_s(_tm, _t)
#define AvGmTimeS(_t,_tm) gmtime_s(_tm,_t)
#else
#define AppRunMSec(msec)\
	do {												\
		struct timespec spec;							\
		clock_gettime(CLOCK_MONOTONIC, &spec);			\
		msec = spec.tv_sec*1000+spec.tv_nsec/1000000;	\
	} while (0)

#define AvGetTimeOfDay gettimeofday
#define AvLocalTimeS(_t,_tm) localtime_r(_t, _tm)
#define AvGmTimeS(_t,_tm) gmtime_r(_t,_tm)
#endif

//##strings
#if defined(_WINDOWS)
#if _MSC_VER < 1400
#ifndef snprintf
#define snprintf _snprintf
#endif
#else
#ifndef snprintf
#define snprintf sprintf_s
#endif
#endif

#ifndef strcasecmp
#define strcasecmp _stricmp
#endif

#ifndef strncasecmp
#define strncasecmp _strnicmp
#endif
#endif

//##FILES

#if defined(_WINDOWS)
#ifndef socklen_t
#define socklen_t int
#endif

#ifndef F_OK
#define F_OK 0
#endif
#ifndef R_OK
#define R_OK 4
#endif
#ifndef W_OK
#define W_OK 2
#endif
#ifndef X_OK
#define X_OK 1
#endif
#define _filelength_byfd(fd, length) length = _filelength(fd)
#else
#define INVALID_SOCKET -1
#define _open	open
#define _read	read
#define _write	write
#define _close  close
#define _lseek  lseek
#define _chmod  chmod
#define _access access
#define closesocket close
#define _unlink unlink
#define O_BINARY O_SYNC //为了与windws平台兼容 binary这个结构
#define _filelength_byfd(fd, length)\
		do {\
			struct stat filestat;\
			length = -1;\
			if (0 == fstat(fd, &filestat)) length = filestat.st_size;\
				else perror("why lstat error\n");\
		} while (0)
#endif



#if defined(WIN32)
#define AvMSleep(ms) Sleep(ms)
#else
#define AvMSleep(ms) usleep(ms * 1000);
#endif








typedef enum _ {
	__MsgConnect = 0,
	__MsgPushLog = 1,
	__MsgLogIn = 2,
	///主要用于客户端 手机使用--
	__MsgLogInAccount = 3,
	__MsgRegister = 4,
	__MsgKeepAlive = 5,
	__MsgKeepConnect = 6,
	__MsgSystemStatus = 20,
	__MsgSyncSystemTime = 21,
	__MsgDeviceCaps = 22,
	__MsgSystemTimeProfile = 23,
	///主要用于外网客户端---
	__MsgDeviceStat = 24,
	__MsgEnCodeCaps = 40,
	__MsgEnCodeProfile = 41,
	__MsgCaptureCaps = 42,
	__MsgCaptureProfile = 43,
	__MsgImageCaps = 44,
	__MsgImageProfile = 45,
	__MsgCoverProfile = 46,
	__MsgOverLayCaps = 47,
	__MsgOverLayProfile = 48,
	__MsgAudioCaps = 50,
	__MsgAudioProfile = 51,
	__MsgSerialCaps = 60,
	__MsgSerialProfile = 61,
	__MsgPtzCmd = 62,
	__MsgPtzAdvancedMoveCaps = 63,
	__MsgPtzAdvancedMovePresetProfile = 64,
	__MsgPtzAdvancedMoveAutoTourProfile = 65,
	__MsgPtzAdvancedCameraLensCaps = 66,
	__MsgPtzAdvancedCameraLensProfile = 67,
	__MsgDevicesSearch = 80,
	__MsgManufacturerProfile = 81,
	__MsgMediaCtrl = 90,
	__MsgMediaData = 91,
	__MsgSnapshot = 92,
	__MsgNetCommCaps = 100,
	__MsgNetCommProfile = 101,
	__MsgNetCommStatus = 102,
	__MsgWifiManageGetEnvList = 103,
	__MsgWifiManageConfigAll = 104,
	__MsgUserList = 110,
	__MsgUserAdd = 111,
	__MsgUserDel = 112,
	__MsgUserModify = 113,
	__MsgLogList = 120,
	__MsgLogCaps = 121,
	__MsgLogProfile = 122,
	///程序开发者的输出打印语句---
	__MsgDevLogCtrl = 123,
	__MsgDevLog = 124,
	__MsgDevLogList = 125,
	__MsgDevLogProfile = 126,
	__MsgDiskStatus = 130,
	__MsgRecordFileSearch = 131,
	__MsgRecordFileDownload = 132,
	__MsgRecordFileDownloadCtrl = 133,
	__MsgRecordFileDownloadData = 134,
	__MsgSFtpProfile = 150,
	__MsgSmtpProfile = 151,
	__MsgUpnpProfile = 152,
	__MsgNtpProfile = 153,
	__MsgPppoeProfile = 154,
	__MsgRtspProfile = 155,
	__MsgDdnsProfile = 156,
	__MsgP2pProfile = 157,
	__MsgRtmpProfile = 158,
	__MsgSystemVersion = 181,
	__MsgFirmwareVersion = 182,
	__MsgFirmwareUpgrade = 183,
	__MsgFirmwareData = 184,
	__MsgFirmwareUpgradeProgress = 185,
	__MsgEventCaps = 190,
	__MsgEventCtrl = 191,
	__MsgEventMsg = 192,
	__MsgIMaCaps = 220,
	__MsgIMaFaceDetecProfile = 221,
	__MsgIMaFaceRecogniProfile = 222,
	__MsgIMaPlateRecogniProfile = 223,
	__MsgIMaMotionFollowProfile = 224,
	__MsgWanRouter = 300,
	__MIN = __MsgConnect,
	__MAX = __MsgWanRouter
}__Msg;



//以下打印语句是库的主体在使用，发包的时候可以屏蔽-----
#if 1
#define _logDebugMsg(fmt,...)
#define _logDebugWarn(fmt,...)
#define _logDebugErr(fmt,...)
#define _logDebugHint(fmt,...)

#else
#define _logDebugMsg(fmt,...) printf("\033[1;32m%s:%04d]]" fmt "\033[m",__FILE_NAME_, __LINE__, ##__VA_ARGS__)
#define _logDebugWarn(fmt,...) printf("\033[1;33m%s:%04d]]" fmt "\033[m",__FILE_NAME_, __LINE__, ##__VA_ARGS__)
#define _logDebugErr(fmt,...) printf("\033[0;31m%s:%04d]]" fmt "\033[m",__FILE_NAME_, __LINE__, ##__VA_ARGS__)
#define _logDebugHint(fmt,...) printf("\033[0;31m%s:%04d]]" fmt "\033[m",__FILE_NAME_, __LINE__, ##__VA_ARGS__)
#endif



#endif //endif file
